/* @(#)GSLogWriter.java
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Janus Liu    2005-12-22 建立
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.lib;

import java.util.Enumeration;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * 系统日志处理公共类
 */
public class GSLogWriter {
    private static org.apache.log4j.Logger sLogger = null;

    private static Level sDefaultLogLevel = Level.FATAL;

    /**
     * 初始化日志实例
     * 
     * @return
     */
    public static void init(Logger aLogger) {
        init(aLogger, Level.FATAL);
    }

    /**
     * 初始化日志实例
     * 
     * @return
     */
    public static void init(Logger aLogger, Level aDefaultLogLevel) {
        sLogger = aLogger;
        sDefaultLogLevel = aDefaultLogLevel;

        // 1、设置不额外输出，避免RootLogger重复输出应用系统的日志
        if (sLogger != null) {
            sLogger.setAdditivity(false);
        }

        // 2、初始化RootLogger，避免其它开源软件抛出log4j没有初始化的警告信息
        defaultInitial();
    }

    /**
     * 记录调试信息
     * 
     * @param aMsg
     *            调试信息
     */
    public static void debug(String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.debug(aMsg);
    }

    /**
     * 记录调试信息
     * 
     * @param aLogger
     * @param aObject
     * @param aMsg
     */
    public static void debug(Logger aLogger, Object aObject, String aMsg) {
        if (aLogger == null) {
            info("[" + aObject.getClass().getName() + "]:" + aMsg);
        } else {
            aLogger.info("[" + aObject.getClass().getName() + "]:" + aMsg);
        }
    }

    /**
     * 记录调试信息
     * 
     * @param aObject
     *            调试对象
     * @param aMsg
     *            调试信息
     */
    public static void debug(Object aObject, String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.debug("[" + aObject.getClass().getName() + "]:" + aMsg);
    }

    /**
     * 记录调试信息
     * 
     * @param aObject
     *            调试对象
     * @param aMsg
     *            调试信息
     */
    public static void debug(Object aObject, Throwable aThrowable) {
        if (sLogger == null)
            defaultInitial();
        sLogger.debug(aObject, aThrowable);
    }

    /**
     * 记录基本信息
     * 
     * @param aMsg
     *            基本信息
     */
    public static void info(String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.info(aMsg);
    }

    /**
     * 记录基本信息
     * 
     * @param aLogger
     * @param aObject
     * @param aMsg
     */
    public static void info(Logger aLogger, Object aObject, String aMsg) {
        if (aLogger == null) {
            info("[" + aObject.getClass().getName() + "]:" + aMsg);
        } else {
            aLogger.info("[" + aObject.getClass().getName() + "]:" + aMsg);
        }
    }

    /**
     * 记录基本信息
     * 
     * @param aObject
     *            调试对象
     * @param aMsg
     *            基本信息
     */
    public static void info(Object aObject, String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.info("[" + aObject.getClass().getName() + "]:" + aMsg);
    }

    /**
     * 记录警告信息
     * 
     * @param aMsg
     *            警告信息
     */
    public static void warn(String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.warn(aMsg);
    }

    /**
     * 记录警告信息
     * 
     * @param aObject
     *            调试对象
     * @param msg
     *            警告信息
     */
    public static void warn(Object aObject, String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.warn("[" + aObject.getClass().getName() + "]:" + aMsg);
    }

    /**
     * 记录错误信息
     * 
     * @param aMsg
     *            错误信息
     */
    public static void error(String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.error(aMsg);
    }

    /**
     * 记录错误信息
     * 
     * @param aLogger
     * @param aLocalizedMessage
     * @param aMsg
     */
    public static void error(Logger aLogger, String aLocalizedMessage, String aMsg) {
        if (aLogger == null) {
            error(aMsg + " - " + aLocalizedMessage);
        } else {
            aLogger.error(aMsg + " - " + aLocalizedMessage);
        }
    }

    /**
     * 记录错误信息
     * 
     * @param aLogger
     * @param aObject
     * @param aMsg
     */
    public static void error(Logger aLogger, Object aObject, String aMsg) {
        if (aLogger == null) {
            error("[" + aObject.getClass().getName() + "]:" + aMsg);
        } else {
            aLogger.error("[" + aObject.getClass().getName() + "]:" + aMsg);
        }
    }

    /**
     * 记录错误信息
     * 
     * @param aObject
     *            调试对象
     * @param aMsg
     *            错误信息
     */
    public static void error(Object aObject, String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.error("[" + aObject.getClass().getName() + "]:" + aMsg);
    }

    /**
     * 记录调试信息
     * 
     * @param aObject
     *            调试对象
     * @param aMsg
     *            调试信息
     */
    public static void error(Object aObject, Throwable aThrowable) {
        if (sLogger == null)
            defaultInitial();
        sLogger.error(aObject, aThrowable);
    }

    /**
     * 记录失败信息
     * 
     * @param aMsg
     *            失败信息
     */
    public static void fatal(String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.fatal(aMsg);
    }

    /**
     * 记录失败信息
     * 
     * @param aObject
     *            调试对象
     * @param aMsg
     *            失败信息
     */
    public static void fatal(Object aObject, String aMsg) {
        if (sLogger == null)
            defaultInitial();
        sLogger.fatal("[" + aObject.getClass().getName() + "]:" + aMsg);
    }

    /**
     * 系统默认初始化方法
     */
    private static void defaultInitial() {
        Logger tLogger = org.apache.log4j.Logger.getRootLogger();
        tLogger.removeAllAppenders();
        if (sLogger != null) {
            // 如果应用系统已经初始化log4j的实例，则RootLogger使用相同的配置
            Enumeration tAppenders = sLogger.getAllAppenders();
            while (tAppenders.hasMoreElements())
                tLogger.addAppender((Appender) tAppenders.nextElement());
            tLogger.setLevel(sDefaultLogLevel);
        } else {
            try {
            // 应用系统未初始化log4j的实例，则RootLogger默认的Console输出
            PatternLayout tPatternLayout = new PatternLayout();
            tPatternLayout.setConversionPattern("Default:%d{yy/MM/dd HH:mm:ss.SSS}[%4.4t][%-5p]%m%n");
            Appender tConsoleAppender = new ConsoleAppender(tPatternLayout, "System.out");
            tConsoleAppender.setName("ConsoleAppender");
            tLogger.addAppender(tConsoleAppender);
            tLogger.setLevel(Level.INFO);
            sLogger = tLogger;
            }
            catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
    }
}
